home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / test / test4.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  1KB  |  76 lines

  1. /* test 4 */
  2.  
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7.  
  8. int pid0, pid1, pid2, pid3, s;
  9. int i, fd;
  10. int nextb;
  11. char *tempfile = "test4.temp";
  12. char buf[1024];
  13.  
  14. main()
  15. {
  16.   int k;
  17.  
  18.   creat(tempfile, 0777);
  19.   printf("Test  4 ");
  20.   fflush(stdout);        /* have to flush for child's benefit */
  21.  
  22.   for (k = 0; k < 20; k++) {
  23.     subr();
  24.   }
  25.   printf("ok\n");
  26.   unlink(tempfile);
  27. }
  28.  
  29.  
  30. subr()
  31. {
  32.   if (pid0 = fork()) {
  33.     /* Parent 0 */
  34.     if (pid0 < 0) nofork();
  35.     if (pid1 = fork()) {
  36.         /* Parent 1 */
  37.         if (pid1 < 0) nofork();
  38.         if (pid2 = fork()) {
  39.             /* Parent 2 */
  40.             if (pid2 < 0) nofork();
  41.             if (pid3 = fork()) {
  42.                 /* Parent 3 */
  43.                 if (pid3 < 0) nofork();
  44.                 for (i = 0; i < 10000; i++);
  45.                 kill(pid2, 9);
  46.                 kill(pid1, 9);
  47.                 kill(pid0, 9);
  48.                 wait(&s);
  49.                 wait(&s);
  50.                 wait(&s);
  51.                 wait(&s);
  52.             } else {
  53.                 fd = open(tempfile, O_RDONLY);
  54.                 lseek(fd, 20480L * nextb, 0);
  55.                 for (i = 0; i < 10; i++) read(fd, buf, 1024);
  56.                 nextb++;
  57.                 close(fd);
  58.                 exit(0);
  59.             }
  60.         } else {
  61.             while (1) getpid();
  62.         }
  63.     } else {
  64.         while (1) getpid();
  65.     }
  66.   } else {
  67.     while (1) getpid();
  68.   }
  69. }
  70.  
  71. nofork()
  72. {
  73.   printf("Fork failed.  Not enough memory.\n");
  74.   exit(1);
  75. }
  76.